/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.StringUtils;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.business.dao.CarArchivesMapper;
import com.rf.business.model.CarArchives;
import com.rf.business.model.CarArchivesExample;
import com.rf.business.service.CarArchivesService;

@Service("carArchivesService")
public class CarArchivesServiceImpl implements CarArchivesService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private CarArchivesMapper carArchivesMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param carArchivesId
	 * @return CarArchives
	 */
   	@Override
	public CarArchives getCarArchivesByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return carArchivesMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据CarArchives获取数据
	 * @param CarArchives
	 * @return List<CarArchives>
	 */
	@Override
	public List<CarArchives> getCarArchives(CarArchives carArchives) throws Exception{
		// TODO Auto-generated method stub
		CarArchivesExample example = new CarArchivesExample();
		com.rf.business.model.CarArchivesExample.Criteria criteria= example.createCriteria();
		if(carArchives.getArchiveNo()!=null && !carArchives.getArchiveNo().equals("")){
			criteria.andArchiveNoEqualTo(carArchives.getArchiveNo());
		}
		if(carArchives.getCarNo()!=null && !carArchives.getCarNo().equals("")){
			criteria.andCarNoEqualTo(carArchives.getCarNo());
		}
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<CarArchives> list = carArchivesMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<CarArchives>
	 */
	@Override
	public List<CarArchives> getCarArchives() throws Exception{
		// TODO Auto-generated method stub
		CarArchivesExample example = new CarArchivesExample();
		com.rf.business.model.CarArchivesExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return carArchivesMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param CarArchives
	 * @return
	 */
	@Override
	public void addCarArchives(CarArchives carArchives) throws Exception{
		// TODO Auto-generated method stub
		carArchivesMapper.insert(carArchives);
	}

	/**
	 * 修改数据
	 * @param CarArchives
	 * @param
	 */
	@Override
	public void updateCarArchives(CarArchives carArchives) throws Exception{
		// TODO Auto-generated method stub
		carArchivesMapper.updateByPrimaryKeySelective(carArchives);
	}
	
	/**
	 * 删除数据
	 * @param carArchivesId
	 * @param
	 */
	@Override
	public void deleteCarArchives(String id
) throws Exception{
		// TODO Auto-generated method stub
		carArchivesMapper.deleteByPrimaryKey(id
);
	}

	/**
	 * 获取数据分页
	 * @param CarArchives
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getCarArchivesPage(CarArchives carArchives,PageUtils pageUtils) throws Exception{
//荆有玺20161207，下面注释的内容仅能用于单表的查询，不能用于多表的联合，多表联合需要自己去写SQL
//		CarArchivesExample example = new CarArchivesExample();
//		com.rf.business.model.CarArchivesExample.Criteria criteria= example.createCriteria();
//		if(carArchives.getCarNo() != null && !carArchives.getCarNo().equals("")){
//			criteria.andCarNoLike("%"+carArchives.getCarNo()+"%");
//		}
//		if(carArchives.getArchiveNo()!=null && !carArchives.getArchiveNo().equals("")){
//			criteria.andArchiveNoLike("%"+carArchives.getArchiveNo()+"%");
//		}
		//自己编写SQL，对应JSP页面中的名称应该与数据库查询的名称一致，要求大写。
		StringBuffer sql =new StringBuffer("select c.id,c.car_no,c.archive_no,c.serial_no,c.car_nationality,"
				+ "c.car_owner,c.car_use_type,c.vehicle_import_id,c.begin_date,"
				+ "c.end_date,c.create_time,e.name "
				+ "from BUS_CAR_ARCHIVES c left join BUS_DEP_USER d on c.create_user = d.user_id "
				+ "left join BUS_DEPARTMENT e on d.dep_id = e.id where 1=1");
		//过滤车牌号
		if(StringUtils.isNotBlank(carArchives.getCarNo())){
			sql.append(" AND c.CAR_NO like '%"+carArchives.getCarNo()+"%'");
		}
		//过滤车辆备案号
		if(StringUtils.isNotBlank(carArchives.getArchiveNo())){
			sql.append(" AND c.ARCHIVE_NO like '%"+carArchives.getArchiveNo()+"%'");
		}
		sql.append(" order by c.create_time desc ");
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
//		List<CarArchives> list = carArchivesMapper.selectByExample(example);//此句用于单表查询时使用
		List<Map<String, Object>> list=sqlAdapterMapper.selectSQL(sql.toString());//此句用于自定义SQL查询时使用
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
}

